home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-12-12 | 6.2 KB | 212 lines | [TEXT/CWIE] |
- /*
- LassoProxy.java
-
- Communicates with Lasso running on the web server to process FileMaker Pro requests.
-
- Copyright © 1996 Blue World Communications, Inc.. All rights reserved.
- */
- package blueworld.lasso;
-
- import java.util.*;
- import java.net.*;
- import java.io.*;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.image.ImageProducer;
-
- public class LassoProxy
- {
- private URL fBaseURL;
- private String fLasso;
- boolean debug = true;
-
- public LassoProxy( URL baseURL, String lasso )
- {
- fBaseURL = baseURL;
- fLasso = new String(lasso);
- }
-
- public LassoProxy(URL base)
- { this(base, "action,lasso"); }
-
- static BitSet dontNeedEncoding;
-
- /* The list of characters that are not encoded have been determined by
- referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
-
- static {
- dontNeedEncoding = new BitSet(256);
- int i;
- for (i = 'a'; i <= 'z'; i++) {
- dontNeedEncoding.set(i);
- }
- for (i = 'A'; i <= 'Z'; i++) {
- dontNeedEncoding.set(i);
- }
- for (i = '0'; i <= '9'; i++) {
- dontNeedEncoding.set(i);
- }
- dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
- dontNeedEncoding.set('-');
- dontNeedEncoding.set('_');
- dontNeedEncoding.set('.');
- dontNeedEncoding.set('*');
- }
- public static String URLencodeForUTF(String s) {
- int maxBytesPerChar = 10;
- ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
-
- for (int i = 0; i < s.length(); i++) {
- int c = (int)s.charAt(i);
- if (dontNeedEncoding.get(c)) {
- if (c == ' ') {
- c = '+';
- }
- out.write(c);
- } else {
- out.write('%');
- out.write(Character.forDigit((c >> 4) & 0xF, 16));
- out.write(Character.forDigit(c & 0xF, 16));
- }
- }
-
- return out.toString();
- }
- // First UTF-8 encodes, then URL encodes the input string
- public static String prepare(String in) throws IOException
- { return URLencodeForUTF(UTF8Coder.encode(in)); }
-
- public String[] databaseNames() throws IOException
- {
- return getNames( new URL( fBaseURL, fLasso + "?[dbnames]" ) );
- }
-
- public String[] layoutNames( String databaseName ) throws IOException
- {
- return getNames( new URL( fBaseURL, fLasso + "?[database]=" +
- LassoProxy.prepare(databaseName) +
- "&[layoutnames]" ) );
- }
- // request contains the recid, databasename and layout name.
- // fieldName is the name of the field which contains the image
- // bitsPerPixel can be either 16 or 32
- // imageQuality can be from 0 to 4
- public Image getJPEG(LassoRequest request, String fieldName, int bitsPerPixel, int imageQuality)
- throws MalformedURLException, IOException
- {
- // must have set a recid for this to work
- if (request.recordID() < 0)
- return null;
- if (bitsPerPixel > 32) bitsPerPixel = 32;
- if (bitsPerPixel < 16) bitsPerPixel = 16;
- if (imageQuality < 0) imageQuality = 0;
- if (imageQuality > 4) imageQuality = 4;
-
- StringBuffer requestString = makeImageString(request);
-
- requestString.append("&").append(LassoProxy.prepare(fieldName)).append("=jpeg,").append(bitsPerPixel);
- requestString.append(",").append(imageQuality);
-
- return Toolkit.getDefaultToolkit().createImage((ImageProducer)(new URL(fBaseURL, requestString.toString())).getContent());
- }
-
- // request contains the recid, databasename and layout name.
- // fieldName is the name of the field which contains the image
- // bitsPerPixel can be either 1, 2, 4 or 8
- public Image getGIF(LassoRequest request, String fieldName, int bitsPerPixel, boolean interlace)
- throws MalformedURLException, IOException
- {
- // must have set a recid for this to work
- if (request.recordID() < 0)
- return null;
- if (bitsPerPixel > 8) bitsPerPixel = 8;
- if (bitsPerPixel < 1) bitsPerPixel = 1;
-
- StringBuffer requestString = makeImageString(request);
-
- requestString.append("&").append(LassoProxy.prepare(fieldName)).append("=gif,");
- requestString.append(bitsPerPixel).append(",").append((interlace) ? "true" : "false");
- URL url = new URL(fBaseURL, requestString.toString());
- return Toolkit.getDefaultToolkit().createImage((ImageProducer)(new URL(fBaseURL, requestString.toString())).getContent());
- }
-
- protected StringBuffer makeImageString(LassoRequest req) throws IOException
- {
- StringBuffer requestString = new StringBuffer(getLasso());
- requestString.append("?[image]&[database]=").append(LassoProxy.prepare(req.databaseName()));
- if (req.layoutName().length() > 0)
- requestString.append("&[layout]=").append(LassoProxy.prepare(req.layoutName()));
- requestString.append("&[recid]=").append(req.recordID());
- return requestString;
- }
-
- // return the current Lasso name
- public String getLasso()
- { return fLasso; }
-
- public LassoResponse processRequest( LassoRequest request ) throws IOException
- {
- URL requestURL = new URL(fBaseURL, request.toString());
- // URL requestURL = new String(request.toString());
- if(debug == true)
- System.out.println("request is: " + requestURL);
- String response = sendHTTPRequest(requestURL);
- if(debug == true)
- System.out.println("response is: " + response);
- return ( new LassoResponse( response ) );
- }
-
- private String[] getNames( URL url ) throws IOException
- {
- String response = sendHTTPRequest( url );
- StringTokenizer tokenizer = new StringTokenizer( response.toString(), "È\r\n" );
- int numNames = tokenizer.countTokens();
- String names[] = new String[ numNames ];
-
- for ( int i = 0; i < numNames; i++ )
- names[ i ] = tokenizer.nextToken();
-
- return names;
- }
-
- private String sendHTTPRequest( URL url ) throws IOException
- {
- URLConnection connection = url.openConnection();
-
- connection.setUseCaches( false );
- connection.setAllowUserInteraction( true ); // experimental
-
- int inputChar;
- InputStream response = connection.getInputStream(); // send HTTP request
- StringBuffer buffer = new StringBuffer();
- String result = null;
-
- try
- {
- try
- {
- while ( (inputChar = response.read()) != -1)
- buffer.append((char)inputChar);
- } catch(IOException e) {}
- result = UTF8Coder.decode(buffer.toString());
- }
- catch(IOException e)
- { System.err.println(e.toString()); result = new String(); }
-
- response.close();
-
- return result;
- }
-
- public void setDebug(boolean state)
- {
- debug = state;
- }
-
- public boolean getDebug()
- {
- return debug;
- }
- }
-
-